package com.lzh.farmersshop.controller.mall;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lzh.farmersshop.entity.Shoppcart;
import com.lzh.farmersshop.entity.Userinfo;
import com.lzh.farmersshop.service.ShoppcartService;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * (Shoppcart)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:51
 */
@Slf4j
@RestController
@RequestMapping("mall/cart")
@Transactional(rollbackFor = Exception.class)
public class CartController {
    /**
     * 服务对象
     */
    @Resource
    private ShoppcartService shoppcartService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return Result.success(shoppcartService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param shoppcart 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result insert(HttpServletRequest request, Shoppcart shoppcart) {
        //获取用户信息
        if (request.getSession().getAttribute("userinfo")== null) {
            return Result.failure(401,"请先登录！！！");
        }
        //判断参数是否为空shoppcart
        if (shoppcart == null) {
            return Result.failure("参数为空！！！");
        }
        Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
        //先查询该用户购物车中有没有这个商品，有的话进行累加，没有的话添加
        Shoppcart one = shoppcartService.getOne(new QueryWrapper<Shoppcart>().eq("userid", userinfo.getId()).eq("goodsid", shoppcart.getGoodsid()));
        if (one!=null){
            //累加
            one.setNum(shoppcart.getNum()+one.getNum());
            one.setTime(new Date());
            if (shoppcartService.updateById(one)){
                return Result.success("加入购物车成功");
            }else{
                return Result.failure("加入购物车失败");
            }
        }

        //添加购物车
        shoppcart.setUserid(userinfo.getId());
        shoppcart.setTime(new Date());
        shoppcart.setIsdelete(1);
        if (shoppcartService.save(shoppcart)){
            return Result.success("加入购物车成功");
        }
        return Result.success("加入购物车失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @PostMapping("deletes")
    public Result deletes(HttpServletRequest request, @RequestParam(value = "ids[]",required = false) Integer[] ids) {
        //获取用户信息
        if (request.getSession().getAttribute("userinfo")== null) {
            return Result.failure(401,"请先登录！！！");
        }
        //判断id是否为空
        if (ids == null) {
            return Result.failure("参数为空！！！");
        }

        UpdateWrapper<Shoppcart> shoppcartUpdateWrapper = new UpdateWrapper<>();
        shoppcartUpdateWrapper.in("id",ids);
        shoppcartUpdateWrapper.set("isdelete",2);

        if (shoppcartService.update(shoppcartUpdateWrapper)){
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }
}

